#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;


class Solution {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<char, int> hashPattern;
        unordered_map<char, int> hashS;
        for (auto e : pattern) {
            hashPattern[e]++;
        }
        for (auto e : s) {
            if (hashPattern.count(e)) {
                hashS[e]++;
            }
        }
        for (auto& e : hashPattern) {
            if (e.second != hashS[e.first]) {
                cout << e.first << endl;
                cout << e.second << " " << hashS[e.first] << endl;;
                return false;
            }
        }
        return true;
    }
};


class SolutionTrue {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<char, string> hash;
        unordered_map<string, char> revHash;
        int posi = 0;
        int len = s.size();
        int poStart = 0;
        int count = 0;
        while (posi != len) {
            if (s[posi] == ' ') {
                string sbStr = s.substr(poStart, posi - poStart);
                if (revHash.count(sbStr)) {
                    if (revHash[sbStr] != pattern[count]) {
                        return false;
                    }
                }
                else {
                    revHash[sbStr] = pattern[count];
                }
                if (hash.count(pattern[count])) {
                    if (hash[pattern[count]] != sbStr) {
                        return false;
                    }
                }
                else {
                    hash[pattern[count]] = sbStr;
                }
                count++;
                poStart = posi + 1;
            }
            posi++;
        }
        string sbStr = s.substr(poStart, posi - poStart);
        if (hash.count(pattern[count])) {
            if (hash[pattern[count]] != sbStr) {
                return false;
            }
        }
        if (revHash.count(sbStr)) {
            if (revHash[sbStr] != pattern[count]) {
                return false;
            }
        }
        if (pattern.size() != count + 1) {
            return false;
        }
        return true;
    }
};